﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.Web.Caching;

namespace Directory.Web
{
	/// <summary>
	/// CategoryDataProxy 的摘要说明
	/// </summary>
	public sealed class CategoryDataProxy
	{
		private CategoryDataProxy() { }

		private static readonly int categorycountTimeout = int.Parse(ConfigurationManager.AppSettings["CategoryCountCacheDuration"]);

		public static IList<Directory.Model.CategoryInfo> GetChildsOfCategory(int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.GetChildsOfCategory(categoryId);
		}

		public static IList<Directory.Model.CategoryInfo> GetRootCategoriesForUser(string username)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			if (RoleDataProxy.IsUserInRole(username, WebSettings.GetInstance().RootCategoryAdminRole))
			{
				return GetChildsOfCategory(WebSettings.GetInstance().RootCategoryId);
			}
			else
			{
				return bll.GetRootCategoriesForUser(username);
			}
		}

		public static bool DeleteCategory(int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.TryDeleteCategory(categoryId);
		}

		public static IList<Directory.Model.CategoryUser> GetUsersForCategory(int categoryId, int pageIndex, int pageSize, out int totalRecords)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.GetUsersForCategory(categoryId, pageIndex, pageSize, out totalRecords);
		}

		public static void AddUserToCategory(string username, int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			bll.AddUserToCategory(username, categoryId);
		}

		public static void RemoveUserFromCategory(string username, int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			bll.RemoveUserFromCategory(username, categoryId);
		}

		public static Directory.Model.CategoryInfo CreateCategory(string categoryName, int parentCategoryID, string description, string marker, int order, out Directory.Model.CategoryInfo.ErrorCode errorCode)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.CreateCategory(categoryName, parentCategoryID, description, marker, order, out errorCode);
		}

		public static Directory.Model.CategoryInfo GetCategory(int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.GetCategoryInfo(categoryId);
		}

		public static void UpdateCategoryInfo(Directory.Model.CategoryInfo category, out Directory.Model.CategoryInfo.ErrorCode errorCode)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			bll.UpdateCategory(category, out errorCode);
		}

		public static IList<Directory.Model.CategoryInfo> GetAllCategoriesForUser(string username, string prefix)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			IList<Directory.Model.CategoryInfo> rawList;
			if (RoleDataProxy.IsUserInRole(username, WebSettings.GetInstance().RootCategoryAdminRole))
			{
				rawList = bll.GetRecursiveChildsOfCategory(WebSettings.GetInstance().RootCategoryId, -1);
			}
			else
			{
				rawList = bll.GetRecursiveChildCategoriesForUser(username);
			}
			for (int i = 0; i < rawList.Count; i++)
			{
				rawList[i].CategoryName = GetPrefix(prefix, rawList[i].Depth) + rawList[i].CategoryName;
			}
			return rawList;
		}

		public static IList<Directory.Model.CategoryInfo> GetRecursiveChildsOfCategory(int categoryId, int depth)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.GetRecursiveChildsOfCategory(categoryId, depth);
		}

		public static IList<Directory.Model.CategoryInfo> GetRecursiveParentsOfCategory(int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.GetRecursiveParentsOfCategory(categoryId, WebSettings.GetInstance().RootCategoryId);
		}

		public static IList<Directory.Model.CategoryInfo> GetRecommandCategories(int pageIndex, int pageSize, out int totalRecords)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.GetRecommandCategories(pageIndex, pageSize, out totalRecords);
		}

		public static void RecommandCategory(int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			bll.RecommandCategory(categoryId);
		}

		public static void UnrecommandCategory(int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			bll.UnrecommandCategory(categoryId);
		}

		public static int GetCategoriesCount()
		{
			string key = "category_count";
			if (HttpRuntime.Cache[key] == null)
			{
				Directory.BLL.Category bll = new Directory.BLL.Category();
				HttpRuntime.Cache.Add(key, bll.GetCategoriesCount(WebSettings.GetInstance().RootCategoryId), null, DateTime.Now.AddMinutes(categorycountTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
			}
			return int.Parse(HttpRuntime.Cache[key].ToString());
		}

		public static DataSet GetExportDataSet(int categoryId)
		{
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.GetExportDataSet(categoryId);
		}

		public static IList<Directory.Model.CategoryInfo> SearchCategories(string keywords)
		{
			int count = 0;
			Directory.BLL.Category bll = new Directory.BLL.Category();
			return bll.SearchCategories(keywords, 0, 999, out count);
		}

		private static string GetPrefix(string prefix, int times)
		{
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < times; i++)
			{
				sb.Append(prefix);
			}
			return sb.ToString();
		}
	}
}
